package com.common.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	public static int getYear() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		return calendar.get(Calendar.YEAR);
	}

	public static int getMonth() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		return calendar.get(Calendar.MONTH) + 1;
	}

	public static int getDay() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		return calendar.get(Calendar.DATE);
	}

	public static int getLastDay() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DATE, 1);
		calendar.roll(Calendar.DATE, -1);
		return calendar.get(Calendar.DATE);
	}

	public static String getFull() {
		Calendar calendar = Calendar.getInstance();
		return String.format("%1$tY-%<tm-%<te", calendar);
	}

	public static String getWeekMonth() {
		return String.format("%1$tY�?%<tm�?", Calendar.getInstance());
	}

	public static String getDate(Date date){
		String result="";
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		if(date!=null){
			result=sdf.format(date);
		}
		return result;
	}
	
	public static String getDateTime(Date date){
		String result="";
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if(date!=null){
			result=sdf.format(date);
		}
		return result;
	}
	
	public static String getDateTime_Y_M_D_H_0(Date date){
		String result="";
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:00:00");
		if(date!=null){
			result=sdf.format(date);
		}
		return result;
	}
	
	//format格式�?"yyyy�?-MM�?-dd�? HH�?:mm�?:ss�?"
	public static String formatDate(Date date,String format){
		String result="";
		SimpleDateFormat sdf=new SimpleDateFormat(format);
		if(date!=null){
			result=sdf.format(date);
		}
		return result;
	}
	
	public static Date formatString(String str,String format) throws Exception{
		SimpleDateFormat sdf=new SimpleDateFormat(format);
		return sdf.parse(str);
	}
	
	   /**
     * 将日期信息转换成今天、明天、后天、星期
     *
     * @param date
     * @return
     */
    public static String getDateDetail(Date date) {
        if (date == null) {
            return "";
        }
        Calendar today = Calendar.getInstance();
        Calendar target = Calendar.getInstance();
        target.setTime(date);
        int tY = today.get(Calendar.YEAR);
        int tM = today.get(Calendar.MONTH);
        int tD = today.get(Calendar.DAY_OF_MONTH);


        int gY = target.get(Calendar.YEAR);
        int gM = target.get(Calendar.MONTH);
        int gD = target.get(Calendar.DAY_OF_MONTH);

        String ma = getAMPM(target.get(Calendar.HOUR_OF_DAY));
        if (gY == tY && tM == gM) {

            switch (gD - tD) {
                case 0:
                    return TODAY + " " + ma;
                case 1:
                    return TOMORROW + " " + ma;
                case 2:
                    return AFTER_TOMORROW + " " + ma;
                case -1:
                    return YESTERDAY + " " + ma;
                case -2:
                    return BEFORE_YESTERDAY + " " + ma;
                default:
                    return getWeek(target);
            }
        } else {
            // 星期

            return getWeek(target);
        }


    }

    private static String getWeek(Calendar target) {
        String ma = getAMPM(target.get(Calendar.HOUR_OF_DAY));
        int dayForWeek = target.get(Calendar.DAY_OF_WEEK);
        switch (dayForWeek) {
            case 1:
                return SUNDAY + " " + ma;
            case 2:
                return MONDAY + " " + ma;
            case 3:
                return TUESDAY + " " + ma;
            case 4:
                return WEDNESDAY + " " + ma;
            case 5:
                return THURSDAY + " " + ma;
            case 6:
                return FRIDAY + " " + ma;
            case 7:
                return SATURDAY + " " + ma;
            default:
                return "";
        }
    }


    private static String getAMPM(int hour) {
        if (6 <= hour && hour < 8) {
            return "早上";
        } else if (8 <= hour && hour < 11) {
            return "上午";
        } else if (11 <= hour && hour < 13) {
            return "中午";
        } else if (13 <= hour && hour < 18) {
            return "下午";
        } else if (18 <= hour && hour < 23) {
            return "晚上";
        }

        return "凌晨";
    }

    public static final String TODAY = "今天";
    public static final String YESTERDAY = "昨天";
    public static final String TOMORROW = "明天";
    public static final String BEFORE_YESTERDAY = "前天";
    public static final String AFTER_TOMORROW = "后天";
    public static final String SUNDAY = "星期日";
    public static final String MONDAY = "星期一";
    public static final String TUESDAY = "星期二";
    public static final String WEDNESDAY = "星期三";
    public static final String THURSDAY = "星期四";
    public static final String FRIDAY = "星期五";
    public static final String SATURDAY = "星期六";

    /**
     * 以友好的方式显示时间
     *
     * @return
     */
    public static String friendTime(Date time) {

        if (time == null) {
            return "";
        }
        String ftime = "";
        Calendar cal = Calendar.getInstance();
        long lt = time.getTime() / 86400000;
        long ct = cal.getTimeInMillis() / 86400000;
        int days = (int) (ct - lt);
        if (days == 0) {
            int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);
            if (hour == 0)
                ftime = Math.max(
                        (cal.getTimeInMillis() - time.getTime()) / 60000, 1)
                        + "分钟前";
            else
                ftime = hour + "小时前";
        } else if (days == 1) {
            ftime = "昨天";
        } else if (days == 2) {
            ftime = "前天";
        } else if (days > 2 && days <= 10) {
            ftime = days + "天前";
        } else if (days > 10) {
            ftime = dateFormater2.get().format(time);
        }
        return ftime;
    }
    public static String toFormat_Y_M_D_H_M(Date date) {
        if (date == null) {
            return "";
        }
        return dateFormater3.get().format(date);
    }    private final static ThreadLocal<SimpleDateFormat> dateFormater3 = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm");
        }
    };
    private final static ThreadLocal<SimpleDateFormat> dateFormater2 = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd");
        }
    };
}
